Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: l2 metrics api integration #64

Merged
merged 3 commits into from
Sep 4, 2024
Merged

Conversation

0xnigir1
Copy link
Collaborator

@0xnigir1 0xnigir1 commented Sep 3, 2024

🤖 Linear

Closes ZKS-229 ZKS-202

Description

  • integrate L2 Metrics info to metrics/zkchain/:chainId ep
  • add JSON map env. var for configuring L2 chains services

@0xnigir1 0xnigir1 requested review from 0xkenj1 and 0xyaco September 3, 2024 22:25
Copy link

linear bot commented Sep 3, 2024

Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposing an alternative for the sake of correctness in case you consider it adds some value. Looking good!

@@ -18,13 +18,19 @@ const urlArraySchema = z
message: "Must be a comma-separated list of valid URLs",
});

const urlArrayMapSchema = z.record(
z.union([z.coerce.number().int(), z.string().regex(/^\d+$/)]), // key: number or string number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super edge case but would the key "04" be validated with this ruleset? Should we invalidate a chain id starting with a 0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's fine, chain ids are numbers actually but JSON doesn't allow numbers as key type so if you write, "04" to us it's the same as "4"

const env = validationSchema.safeParse(process.env);
const env = validationSchema.safeParse({
...process.env,
L2_RPC_URLS_MAP: JSON.parse(process.env.L2_RPC_URLS_MAP || "{}"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor drawback this JSON.parse here could have is that it might throw when parsing an invalid JSON string, causing the nice summary of validation errors to not be generated.

I was curious to see if we could leverage zod for this; found this conversation that might be useful if you want to move that JSON.parse to a schema.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one here, i used the suggestion in the discussion 🙌

@@ -39,6 +39,7 @@ Available options:
| `SHARED_BRIDGE_ADDRESS` | Shared Bridge address | N/A | Yes | |
| `STATE_MANAGER_ADDRESSES` | CSV list of State manager addresses | N/A | Yes | |
| `L1_RPC_URLS` | CSV list of RPC URLs. For example, `https://eth.llamarpc.com,https://rpc.flashbots.net/fast` | N/A | Yes | You can check [Chainlist](https://chainlist.org/) for a list of public RPCs |
| `L2_RPC_URLS_MAP` | JSON from chain id to CSV list of L2 RPC URLs. For example, {"324":"https://mainnet.era.zksync.io,https://zksync.drpc.org"} | N/A | No | You can check [Chainlist](https://chainlist.org/) for a list of public RPCs |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are already parsing a json don't you think having an array of rpcs is better ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced also L1_RPC_URLS to also be a json array since it's easier to read

Comment on lines +47 to +52
for (const [chainId, rpcUrls] of Object.entries(l2ChainsConfigMap)) {
const provider = new ZKChainProvider(rpcUrls, logger);
const metricsService = new L2MetricsService(provider, logger);
l2MetricsMap.set(BigInt(chainId), metricsService);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was wondering if it would be better to encapsulate this logic inside the L2MetricsService and you just pass the map as parameter. But this approach seems to be more decoupled. 👍

@0xnigir1 0xnigir1 requested review from 0xyaco and 0xkenj1 September 4, 2024 20:54
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool af 😎

Comment on lines -14 to +23
const urlArraySchema = z
.string()
.transform((str) => str.split(","))
.refine((urls) => urls.every((url) => z.string().url().safeParse(url).success), {
message: "Must be a comma-separated list of valid URLs",
});
const urlArraySchema = z.array(z.string().url());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noice

@0xnigir1 0xnigir1 merged commit 9a13844 into dev Sep 4, 2024
6 checks passed
@0xnigir1 0xnigir1 deleted the feat/l2-metrics-integration branch September 4, 2024 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants